package com.yundingai.controller;

import com.yundingai.domain.entity.AllMenu;
import com.yundingai.domain.entity.vo.AllMenuVo;
import com.yundingai.domain.vo.MenuVo;
import com.yundingai.service.AllMenuService;
import com.yundingai.vm.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private AllMenuService allMenuService;

    @GetMapping("/selectAllMenusByUsername")
    public ResponseResult selectAllMenusByUsername(String username) {
        List<AllMenu> allMenus = allMenuService.selectAllMenusByUsername(username);
        return ResponseResult.okResult(allMenus);
    }
    @GetMapping("/selectAllMenusByUserRoleId")
    public ResponseResult selectAllMenusByUserRoleId(@RequestParam String roleId) {
        List<MenuVo> allMenus = allMenuService.selectAllMenusByUserRoleId(roleId);
        return ResponseResult.okResult(allMenus);
    }

    @PutMapping("/updateMenu")
    public ResponseResult updateMenu(@RequestBody AllMenu allMenu) {
        allMenuService.updateMenu(allMenu);
        return ResponseResult.okResult();
    }
@PostMapping("/insertMenu")
    public ResponseResult insertMenu(@RequestBody AllMenuVo AllMenuVo) {
        allMenuService.insertMenu(AllMenuVo);
        return ResponseResult.okResult();
    }

    @GetMapping("/selectUserMenus")
    public ResponseResult selectUserMenus() {
        List<MenuVo> userMenus = allMenuService.selectMenusByCurrentUser();
        return ResponseResult.okResult(userMenus);
    }

    /**
     * 管理员查询所有菜单
     * @return
     */
    @GetMapping("/selectAllMenus")
    public ResponseResult selectAllMenus() {
        List<MenuVo> allMenus = allMenuService.selectAllMenus();
        return ResponseResult.okResult(allMenus);
    }

    /**
     * 用户查询菜单,根据用户上下文获取当前用户的ID
     */
    @GetMapping("/selectAllUserMenus")
    public ResponseResult selectAllUserMenus() {
        List<MenuVo> userMenus = allMenuService.selectMenusByCurrentUser();
        return ResponseResult.okResult(userMenus);
    }

    @GetMapping("/selectMenuById")
    public ResponseResult selectMenuById(@RequestParam Integer menuId) {
        MenuVo menu = allMenuService.selectMenuById(menuId);
        return ResponseResult.okResult(menu);
    }

    @DeleteMapping("/deleteMenu")
    public ResponseResult deleteMenu(@RequestParam Integer menuId) {
        allMenuService.deleteMenu(menuId);
        return ResponseResult.okResult();
    }

    @PostMapping("/insertChildMenu")
    public ResponseResult insertChildMenu(@RequestBody AllMenuVo allMenuVo) {
        allMenuService.insertChildMenu(allMenuVo);
        return ResponseResult.okResult();
    }

}
